package Common;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


public class WebDriver {
    public static  ChromeDriver driver;
    public  static ChromeDriver getDriver(){
        if(driver==null){
            driver=new ChromeDriver();
            //添加隐式等待
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        }
        return driver;
    }
    public static  List<String> getName(){
        //文件名称
        SimpleDateFormat s1=new SimpleDateFormat("yyyyMMdd-HHmmssSS");
        //文件夹名称
        SimpleDateFormat s2=new SimpleDateFormat("yyyy-MM-dd");
        String dirName=s2.format(System.currentTimeMillis());
        String fileName=s1.format(System.currentTimeMillis());
        List<String> list=new ArrayList<>();
        list.add(dirName);
        list.add(fileName);
        return list;
    }
    /**
     * 获取屏幕截图
     */
    public  static void getPho(String str) throws IOException {
        List<String> list=getName();
        //文件路径
        String fileName="./src/test-"+str+"-"+list.get(0)+list.get(1)+".png";
        File file1= driver.getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file1,new File(fileName));
    }
}
